package ptij.vocoder.baseModel.common;

import ptolemy.actor.*;
import ptolemy.kernel.*;
import ptolemy.kernel.util.*;
import ptolemy.data.ObjectToken;
import ptolemy.data.IntToken;
import ptolemy.data.expr.Parameter;
import ptolemy.data.type.*;

import ptij.vocoder.baseModel.*;

//////////////////////////////////////////////////////////////////////////
//// DisassembleFrame
/**

@author Ivan Jeukens
@version v 0.1 07/08/2003
*/
public class DisassembleFrame extends TypedAtomicActor {

    public DisassembleFrame(CompositeEntity container, String name) throws
            IllegalActionException, NameDuplicationException {
        super(container, name);

        input = new TypedIOPort(this, "input", true, false);
        input.setTypeEquals(BaseType.OBJECT);

        output = new TypedIOPort(this, "output", false, true);
        output.setTypeEquals(BaseType.OBJECT);
        
        divideby = new Parameter(this, "divideby", new IntToken(4));
    }

    ///////////////////////////////////////////////////////////////////
    ////                     ports and parameters                  ////

    // Input ports
    public TypedIOPort input;

    // Output Ports
    public TypedIOPort output;

    public Parameter divideby;

    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    public void initialize() throws IllegalActionException {
        super.initialize();

        IntToken it = (IntToken) divideby.getToken();
        _divideby = it.intValue();
    }
    
    public void fire() throws IllegalActionException {
        ObjectToken obj = (ObjectToken) input.get(0);
        Short [] frame = (Short []) obj.getValue();

        for(int j = 0;j < _divideby;j++) {
            Short [] newframe = new Short[frame.length/_divideby];
            for(int i = 0;i < frame.length/_divideby;i++) {
                newframe[i] = frame[(frame.length/_divideby)*j + i];
            }
            output.broadcast(new ObjectToken(newframe));
        }
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private methods                   ////


    ///////////////////////////////////////////////////////////////////
    ////                         private variables                 ////

    private int _divideby;
}

